<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>简单API测试</title>
    <style>
        body { font-family: Arial, sans-serif; margin: 20px; }
        .test-section { margin: 20px 0; padding: 15px; border: 1px solid #ddd; border-radius: 5px; }
        .test-button { padding: 10px 15px; margin: 5px; background: #007bff; color: white; border: none; border-radius: 3px; cursor: pointer; }
        .test-button:hover { background: #0056b3; }
        .result { margin-top: 10px; padding: 10px; background: #f8f9fa; border-radius: 3px; white-space: pre-wrap; }
        .success { background: #d4edda; color: #155724; }
        .error { background: #f8d7da; color: #721c24; }
        .info { background: #d1ecf1; color: #0c5460; }
    </style>
</head>
<body>
    <h1>简单API测试页面</h1>
    
    <div class="test-section">
        <h3>说明</h3>
        <div class="result info">
由于后端接口需要JWT认证，建议按以下步骤测试：

1. 访问主系统：http://localhost:80
2. 使用默认账号登录：
   - 用户名：admin
   - 密码：admin123
3. 登录成功后，导航到"系统管理" → "任务安排"
4. 在任务安排页面测试各项功能

或者使用带认证的测试页面：http://localhost:80/test-api-with-auth.html
        </div>
    </div>
    
    <div class="test-section">
        <h3>1. 测试后端连通性</h3>
        <button class="test-button" onclick="testBackendConnection()">测试后端连通性</button>
        <div id="connectionResult" class="result"></div>
    </div>
    
    <div class="test-section">
        <h3>2. 测试验证码接口</h3>
        <button class="test-button" onclick="testCaptcha()">获取验证码</button>
        <div id="captchaResult" class="result"></div>
    </div>
    
    <div class="test-section">
        <h3>3. 测试登录接口（需要验证码）</h3>
        <button class="test-button" onclick="testLogin()">测试登录</button>
        <div id="loginResult" class="result"></div>
    </div>

    <script>
        const API_BASE = 'http://localhost:8080';
        
        async function testBackendConnection() {
            const resultDiv = document.getElementById('connectionResult');
            resultDiv.textContent = '正在测试后端连通性...';
            
            try {
                const response = await fetch(`${API_BASE}/captchaImage`);
                if (response.ok) {
                    resultDiv.textContent = '✅ 后端服务连通正常';
                    resultDiv.className = 'result success';
                } else {
                    resultDiv.textContent = `❌ 后端服务响应异常: ${response.status}`;
                    resultDiv.className = 'result error';
                }
            } catch (error) {
                resultDiv.textContent = `❌ 无法连接到后端服务: ${error.message}`;
                resultDiv.className = 'result error';
            }
        }
        
        async function testCaptcha() {
            const resultDiv = document.getElementById('captchaResult');
            resultDiv.textContent = '正在获取验证码...';
            
            try {
                const response = await fetch(`${API_BASE}/captchaImage`);
                const data = await response.json();
                
                if (data.code === 200) {
                    resultDiv.textContent = `✅ 验证码获取成功:\nUUID: ${data.uuid}\nBase64图片长度: ${data.img ? data.img.length : 0}`;
                    resultDiv.className = 'result success';
                } else {
                    resultDiv.textContent = `❌ 验证码获取失败: ${data.msg}`;
                    resultDiv.className = 'result error';
                }
            } catch (error) {
                resultDiv.textContent = `❌ 验证码请求失败: ${error.message}`;
                resultDiv.className = 'result error';
            }
        }
        
        async function testLogin() {
            const resultDiv = document.getElementById('loginResult');
            resultDiv.textContent = '正在测试登录...';
            
            try {
                // 先获取验证码
                const captchaResponse = await fetch(`${API_BASE}/captchaImage`);
                const captchaData = await captchaResponse.json();
                
                if (captchaData.code !== 200) {
                    resultDiv.textContent = `❌ 获取验证码失败: ${captchaData.msg}`;
                    resultDiv.className = 'result error';
                    return;
                }
                
                // 尝试登录（使用固定验证码，实际使用时需要用户输入）
                const loginResponse = await fetch(`${API_BASE}/login`, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({
                        username: 'admin',
                        password: 'admin123',
                        code: '1234', // 固定验证码，实际需要用户输入
                        uuid: captchaData.uuid
                    })
                });
                
                const loginData = await loginResponse.json();
                
                if (loginData.code === 200) {
                    resultDiv.textContent = `✅ 登录成功！\nToken: ${loginData.token ? loginData.token.substring(0, 50) + '...' : '无'}`;
                    resultDiv.className = 'result success';
                } else {
                    resultDiv.textContent = `❌ 登录失败: ${loginData.msg}\n\n注意：这通常是因为验证码不正确，请使用主系统登录`;
                    resultDiv.className = 'result error';
                }
            } catch (error) {
                resultDiv.textContent = `❌ 登录请求失败: ${error.message}`;
                resultDiv.className = 'result error';
            }
        }
        
        // 页面加载时自动测试连通性
        window.onload = function() {
            testBackendConnection();
        };
    </script>
</body>
</html>
